package com.simple.ds.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.google.common.collect.Lists;
import com.google.common.primitives.Longs;
import com.simple.common.entity.ReqContext;
import com.simple.common.entity.po.MerchantEntity;
import com.simple.common.entity.po.sys.AppExample;
import com.simple.common.entity.po.sys.Application;
import com.simple.common.entity.po.sys.Merchant;
import com.simple.ds.service.ApiContextService;
import com.simple.ds.service.MerchantService;
import com.simple.ds.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yasud
 * @date 2021年09月19日 13:14
 * @description
 */
@Service
public class ApiContextServiceImpl implements ApiContextService {

    @Autowired
    MerchantService merchantService;

    @Autowired
    ProductService productService;

    @Override
    public ReqContext getFullContextByAppId(String merchantCode) {
        MerchantEntity merchantEntity = merchantService.getById(merchantCode);
        return buildReqContext(merchantEntity);
    }


    private ReqContext buildReqContext(MerchantEntity merchantEntity) {
        Merchant merchant = createMerchant(merchantEntity);
        Application application = createApplication(merchantEntity);
        AppExample example = createExample(merchantEntity);
        ReqContext reqContext = ReqContext.build(merchant, application, example);
        return reqContext;
    }

    private Application createApplication(MerchantEntity merchantEntity) {
        Application application = new Application();
        application.setName(merchantEntity.getMerchantName());
        application.setId(Longs.fromByteArray(DigestUtil.sha256(merchantEntity.getMerchantCode() + "_application")));
        application.setMerchantNo(merchantEntity.getMerchantCode());
        application.setType(Application.Type.APP);
        application.setEnable(merchantEntity.getEnabled());
        application.setCreateTime(merchantEntity.getCreateTime());
        application.setUpdateTime(merchantEntity.getUpdateTime());
        return application;
    }

    private Merchant createMerchant(MerchantEntity merchantEntity) {
        Merchant merchant = new Merchant();
        merchant.setId(Longs.fromByteArray(DigestUtil.sha256(merchantEntity.getMerchantCode())));
        merchant.setIsOpen(merchantEntity.getEnabled());
        merchant.setMerchantNo(merchantEntity.getMerchantCode());
        merchant.setName(merchantEntity.getMerchantName());
        merchant.setPassword("123456");
        merchant.setTemplates(Lists.newArrayList());
        merchant.setType(Merchant.Type.MERCHANT);
        merchant.setCreateTime(merchantEntity.getCreateTime());
        merchant.setLastLoginTime(merchantEntity.getUpdateTime());
        merchant.setUpdateTime(merchantEntity.getUpdateTime());
        return merchant;
    }

    private AppExample createExample(MerchantEntity merchantEntity) {
        AppExample appExample = new AppExample();
        appExample.setId(Longs.fromByteArray(DigestUtil.sha256(merchantEntity.getMerchantCode() + "_example")));
        appExample.setAppId(merchantEntity.getMerchantCode());
        appExample.setEnable(merchantEntity.getEnabled());
        appExample.setName(merchantEntity.getMerchantName());
        appExample.setType(Application.Type.APP);
        appExample.setApplicationId(Longs.fromByteArray(DigestUtil.sha256(merchantEntity.getMerchantCode() + "_application")));
        appExample.setUpdateTime(merchantEntity.getUpdateTime());
        appExample.setCreateTime(merchantEntity.getCreateTime());
        return appExample;
    }
}
